package com.celesea.auth.role.v1.controller;

import com.celesea.auth.role.entity.Role;
import com.celesea.auth.role.entity.RoleCode;
import com.celesea.auth.role.service.RoleService;
import com.celesea.auth.role.v1.controller.request.PageRequest;
import com.celesea.auth.role.v1.controller.request.PostRequest;
import com.celesea.auth.role.v1.controller.request.PutRequest;
import com.celesea.auth.role.v1.controller.request.RolePermissionRequest;
import com.celesea.auth.role.v1.controller.resource.FormResource;
import com.celesea.auth.role.v1.controller.resource.RolePermissionResource;
import com.celesea.auth.role.v1.controller.resource.RoleResource;
import com.celesea.auth.role.vo.RoleFilter;
import com.celesea.auth.role.vo.RolePermissionVo;
import com.celesea.auth.role.vo.RoleVo;
import com.celesea.framework.util.Page;
import com.celesea.framework.util.PageData;
import com.celesea.framework.util.Sort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author JackYuan
 * @create 2017-10-26 23:30
 */
@Api(tags = "角色接口")
@RestController
@RequestMapping("v1/roles")
@PreAuthorize("hasAnyAuthority('" + RoleCode.ADMIN_SYSTEM + "','" + RoleCode.ADMIN_BUS + "')")
public class RoleController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RoleService roleService;

    @GetMapping
    @ApiOperation(value = "角色分页")
    public PageData<RoleResource> page(@Valid PageRequest request, Page page, Sort sort) {
        logger.info("请求参数：{}，分页：{}，排序：{}", request, page, sort);
        PageData<Role> pageData = roleService.selectPage(new RoleFilter(request), page, sort);
        return pageData.convert(item -> new RoleResource(item));
    }

    @PostMapping
    @ApiOperation(value = "保存角色")
    public FormResource post(@RequestBody @Valid PostRequest request) {
        RoleVo role = roleService.insert(new RoleVo(request));
        return new FormResource(role);
    }

    @PutMapping
    @ApiOperation(value = "更新角色")
    public FormResource put(@RequestBody @Valid PutRequest request) {
        RoleVo role = roleService.update(new RoleVo(request));
        return new FormResource(role);
    }

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据id查询角色")
    public FormResource get(@PathVariable String id) {
        RoleVo role = roleService.selectById(id);
        return new FormResource(role);
    }

    @GetMapping(value = "/{id}/permissions")
    @ApiOperation(value = "根据id查询角色授权信息")
    public RolePermissionResource getPermissions(@PathVariable String id) {
        RolePermissionVo permissionVo = roleService.selectPermissions(id);
        return new RolePermissionResource(permissionVo);
    }

    @PutMapping(value = "/{id}/permissions")
    @ApiOperation(value = "根据id修改角色授权信息")
    public RolePermissionResource putPermissions(@PathVariable String id,
                                                 @RequestBody @Valid RolePermissionRequest request) {
        RolePermissionVo permissionVo = roleService.updatePermissions(new RolePermissionVo(id, request));
        return new RolePermissionResource(permissionVo);
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "根据id删除角色")
    public void delete(@PathVariable String id) {
        roleService.doDelete(id);
    }

    @PutMapping(value = "/{id}/disable")
    public void disable(@PathVariable String id) {
        roleService.disable(id);
    }

    @PutMapping(value = "/{id}/enable")
    public void enable(@PathVariable String id) {
        roleService.enable(id);
    }
}
